import { getStore } from '@/utils/store'
import { getAreaObj, uploadType } from '@/utils/util'
import { revertUTF8, EncodeUtf8 } from '@/views/oneStopOffice/utils/index'
const userInfo = getStore('userInfo') || {}

const authAreaAdapt = [userInfo.authCity, userInfo.authArea].filter(Boolean)
const tableConfig = {
	stripe: true,
	border: true,
	columns: [
		{
			label: '所属区',
			prop: 'areaName',
		},
		{
			label: '标题',
			prop: 'policyTitle',
		},
		{
			label: '上传时间',
			prop: 'uploadDate',
		},
		{
			label: '单位名称',
			prop: 'unitName',
		},
		{
			label: '类型',
			prop: 'typeLabel',
		},
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

export const formConfig = {
	inline: false,
	labelPosition: 'top',
	showBtn: true,
	showReset: true,
	backBtnText: '重置',
	column: [
		{
			label: '标题',
			prop: 'policyTitle',
			type: 'input',
			span: 12,
			rules: [
				{
					required: true,
					message: '请输入标题',
					trigger: 'blur',
				},
			],
		},
		// {
		// 	label: '类型',
		// 	prop: 'type',
		// 	propName: 'typeLabel',
		// 	type: 'remoteSelect',
		// 	code: 'WORK_PLAN_TYPE',
		// 	api: 'getDictItemList',
		// 	span: 12,
		// 	rules: [
		// 		{
		// 			required: true,
		// 			message: '请选择类型',
		// 			trigger: 'change',
		// 		},
		// 	],
		// },
		{
			label: '单位名称',
			prop: 'unitName',
			type: 'input',
			span: 12,
			// rules: [
			// 	{
			// 		required: true,
			// 		message: '请输入单位名称',
			// 		trigger: 'blur',
			// 	},
			// ],
		},
		// {
		// 	label: '所属区域',
		// 	prop: 'areas',
		// 	propName: 'areasName',
		// 	type: 'AplidAreaSelect',
		// 	regionGrade: 3,
		// 	checkStrictly: false,
		// 	authArea: true,
		// 	span: 12,
		// 	rules: [
		// 		{
		// 			required: true,
		// 			message: '请选择所属区域',
		// 			trigger: 'change',
		// 		},
		// 	],
		// },
		{
			label: '上传时间',
			prop: 'uploadDate',
			type: 'date',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			disabledDate: (time) => {
				const year = new Date().getFullYear()
				return time.getFullYear() != year
			},
			rules: [
				{
					required: true,
					message: '请选择上传时间',
					trigger: 'change',
				},
			],
			span: 12,
		},
		{
			label: '内容',
			prop: 'policyContent',
			type: 'ueditor',
			span: 24,
			// rules: [
			// 	{
			// 		required: true,
			// 		message: '请输入内容',
			// 		trigger: 'blur',
			// 	},
			// ],
		},
		{
			label: '附件',
			prop: 'policyAttachment',
			type: 'upload',
			fileSize: 100,
			span: 24,
			fileType: uploadType.fileMetaType,
			listType: 'text',
		},
	],
}
const searchConfig = {
	inline: true,
	column: [
		{
			label: '标题',
			prop: 'policyTitle',
			span: 8,
			type: 'input',
		},
		{
			label: '单位名称',
			prop: 'unitName',
			type: 'input',
			span: 8,
		},
	],
}
export const detailConfig = {
	inline: false,
	labelPosition: 'top',
	labelWidth: '200px',
	showBtn: false,
	column: [
		{
			custome: 'HtmlLabel',
			prop: 'policyTitle',
			flag: 'bgTitle',
			labelPosition: 'top',
			span: 24,
		},
		{
			label: '类型',
			prop: 'type	',
			propName: 'typeLabel',
			type: 'remoteSelect',
			code: 'WORK_PLAN_TYPE',
			api: 'getDictItemList',
			span: 8,
		},
		{
			label: '单位名称',
			prop: 'unitName',
			span: 8,
		},
		{
			label: '所属区域',
			prop: 'areasName',
			span: 8,
		},
		{
			label: '上传时间',
			prop: 'uploadDate',
			span: 8,
		},
		{
			label: '内容',
			labelPosition: 'top',
			prop: 'policyContent',
			showWordLimit: true,
			type: 'ueditor',
			height: '20rem',
			span: 24,
		},
		{
			label: '附件',
			prop: 'policyAttachment',
			type: 'upload',
			span: 24,
		},
	],
}

export const api = {
	index: 'view_dpWorkPlan_page',
	add: 'view_dpWorkPlan_add',
	edit: 'view_dpWorkPlan_edit',
	delete: 'view_dpWorkPlan_del',
	detail: 'view_dpWorkPlan_view',
}

export const auth = {
	...api,
}

export const baseConfig: any = {
	pageName: '工作方案',
	addLabel: '新增',
	dictcodes: ['WORK_PLAN_TYPE'],
	rowKey: 'id',
	tipLabel: 'policyTitle',
	api,
	auth: {
		...auth,
	},
	menuBtn: [
		{
			icon: 'editor',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: auth.edit,
			text: true,
			show: true,
		},
		{
			icon: 'detail',
			label: '查看',
			type: 'primary',
			operate: 'detail',
			auth: auth.index,
			text: true,
			show: true,
		},
		{
			icon: 'delete',
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: auth.delete,
			text: true,
			show: true,
		},
	],
	dialogConfig: {
		width: '75%',
	},
	saveFormate: (form) => {
		return new Promise((resolve, reject) => {
			const areasObj: any = getAreaObj(form.areas, '')
			const areasNameObj: any = getAreaObj(form.areasName, '')
			let params = {
				...form,
				...areasObj,
				...areasNameObj,
				areas: undefined,
				areasName: undefined,
			}
			resolve(params)
		})
	},
	beforeOnLoadFormate: async (query) => {
		return new Promise((resolve, reject) => {
			resolve({
				...query,
				year: new Date().getFullYear(),
			})
		})
	},
	operateFormate: async (item, type) => {
		const areaObj: any = getAreaObj(item, '', 'set')
		return Promise.resolve({
			...item,
			areasName: areaObj.names,
			areas: areaObj.areaIds,
		})
	},
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			return {
				...item,
			}
		})
		return Promise.resolve(Object.freeze(tableData))
	},
	searchConfig,
	tableConfig,
	formConfig,
	detailConfig,
}
